package com.klasster.recruitmentPlatform.common.recommendation;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.klasster.recruitmentPlatform.domain.po.Collection;
import com.klasster.recruitmentPlatform.domain.po.Company;
import com.klasster.recruitmentPlatform.domain.po.Job;
import com.klasster.recruitmentPlatform.domain.po.JobIntention;
import com.klasster.recruitmentPlatform.domain.vo.JobHomeVO;
import com.klasster.recruitmentPlatform.service.impl.CollectionServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.CompanyServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.JobIntentionServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.JobServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;

@Component
@RequiredArgsConstructor
public class JobRecommendationUtil {

//    private final JobIntentionServiceImpl jobIntentionService;
    private final JobServiceImpl jobService;
    private final CompanyServiceImpl companyService;
    private final CollectionServiceImpl collectionService;

    private final KPrototypesCluster prototypesCluster;

    public List<JobHomeVO> getRecommendJobs(Long seeker_id, Integer amount) {

        //1.对求职者进行聚类分析，获取所在聚类
        List<Long> seekerIDs = prototypesCluster.getSeekerCluster(seeker_id,4,10);
        seekerIDs.forEach(e -> {
            System.out.print(e+" ");
        });
        //2.获取聚类内用户的收藏职位集合
        Set<Long> jobIDs = new HashSet<>();
        List<Collection> collections = new LinkedList<>();
        seekerIDs.forEach(e -> {
            QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("seeker_id",e).eq("state",1);
            collections.addAll(collectionService.list(queryWrapper));
        });
        int i =0;
        while(jobIDs.size()<amount&&i<collections.size()) {
            jobIDs.add(collections.get(i).getJob_id());
            i++;
        }
        //职位列表
        List<Job> list = new LinkedList<>();
        jobIDs.forEach(e -> {
            QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("state",1).eq("job_id",e);
            list.add(jobService.getOne(queryWrapper));
        });

        // 3.从其他用户收藏中获取的推荐职位不足amount，获取最新的职位推荐进行补充
        if(jobIDs.size()<amount) {
            list.addAll(getCommonRecommendJobs(amount-jobIDs.size(), jobIDs));
        }
        //统一转换数据类型，并补充缺少数据项
        List<JobHomeVO> jobs = BeanUtil.copyToList(list, JobHomeVO.class);
        setCompanyInfo(jobs);
        return jobs;
    }

    public List<Job> getCommonRecommendJobs(Integer amount, Set<Long> neq) {
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        //防止筛选出重复内容
        for (Long id : neq) {
            queryWrapper.ne("job_id", id);
        }
        queryWrapper.eq("state",1).last("limit "+amount);
        return jobService.list(queryWrapper);
    }

    public List<JobHomeVO> getCommonRecommendJobs(Integer amount) {
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",1).last("limit "+amount);
        List<Job> list = jobService.list(queryWrapper);
        List<JobHomeVO> jobs = BeanUtil.copyToList(list, JobHomeVO.class);
        setCompanyInfo(jobs);
        return jobs;
    }

    private void setCompanyInfo(List<JobHomeVO> jobs) {
        //1.归类，将企业相同的职位添加到同一个列表里
        HashMap<Long,List<JobHomeVO>> map = new HashMap<>();
        jobs.forEach(e -> {
            List<JobHomeVO> list = map.getOrDefault(e.getCompany_id(), new LinkedList<>());
            list.add(e);
            map.put(e.getCompany_id(),list);
        });
        //2.对每个企业进行一次查询，再赋值给职位显示对象
        map.forEach((id,list )-> {
            Company company = companyService.getById(id);
            list.forEach(j -> {
                j.setCompany_name(company.getCompany_name());
                j.setCompany_icon(company.getCompany_icon());
                j.setSize(company.getSize());
                j.setIndustry_code(company.getIndustry_code());
            });
        });
    }


}
